#coding:utf-8

from xml.dom import minidom
import datamethod as dm

class TestCase(object):
    def __init__(self, data= [], repeat = 1, patience = 60, rest = 1):
        object.__init__(self)
        self.setData(data)
        self.setPatience(patience)
        self.setRepeat(repeat)
        self.setRest(rest)

    def getBussType(self):
        return self.data[:2]
    
    def getBussData(self):
        return self.data[2:]
    
    def getData(self):
        return self.data
    
    def setData(self, data):
        assert isinstance(data, list)
        self.data = data
        
    def getRest(self):
        return self.rest
    
    def setRest(self, value):
        self.rest = int(value)
        
    def getPatience(self):
        return self.patience
    
    def setPatience(self, value):
        self.patience = int(value)
        
    def getRepeat(self):
        return self.repeat
    
    def setRepeat(self, value):
        self.repeat = int(value)
        
    def getUseTime(self):
        return (self.patience + self.rest) * self.repeat
    
    def loadString(self, strings):
        doc = minidom.parseString(strings)
        caseNode = doc.getElementsByTagName("testcase")[0]
        self.parseXmlNode(caseNode)
        
    def dumpString(self):
        return self.makeXmlNode().toxml('utf-8')
            
    def makeXmlNode(self, doc = None):
        if not doc: 
            doc = minidom.Document()
        caseNode = doc.createElement("testcase")
        data = {"data" : dm.data2str(self.data),
                "repeat" : str(self.repeat),
                "patience" : str(self.patience),
                "rest" : str(self.rest),}
        for key in data:
            keyNode = doc.createElement(key)
            keyNode.appendChild(doc.createTextNode(data[key]))
            caseNode.appendChild(keyNode)
        return caseNode
    
    def parseXmlNode(self, node):
        self.data = dm.str2data(node.getElementsByTagName("data")[0].childNodes[0].data)
        self.repeat = int(node.getElementsByTagName("repeat")[0].childNodes[0].data)
        self.patience = int(node.getElementsByTagName("patience")[0].childNodes[0].data)
        self.rest = int(node.getElementsByTagName("rest")[0].childNodes[0].data)
    
    
class TestFlow(object):
    def __init__(self, name = ""):
        object.__init__(self)
        self.testList = []
        
    def appendTestCase(self, testCase):
        self.testList.append(testCase)
        
    def insertTestCase(self, index, testCase):
        self.testList.insert(index, testCase)
    
    def removeTestCase(self, index):
        return self.testList.pop(index)
    
    def setTestList(self, testList):
        isinstance(testList, list)
        self.testList = testList
        
    def getTestList(self):
        return self.testList
    
    def getCount(self):
        return len(self.testList)
    
    def save(self, path):
        """
        1.将minidom.Element类的writexml方法中代码
                         if self.childNodes:
            writer.write(">%s"%(newl))
            for node in self.childNodes:
                node.writexml(writer,indent+addindent,addindent,newl)
            writer.write("%s</%s>%s" % (indent,self.tagName,newl))
        else:
            writer.write("/>%s"%(newl))
             改为
        if (len(self.childNodes) == 1 and isinstance(self.childNodes[0], Text)) \
                or not self.childNodes:
            writer.write(">")
            if self.childNodes:
                _write_data(writer, self.childNodes[0].data)
            writer.write("</%s>%s" % (self.tagName,newl))
        else:
            writer.write(">%s"%(newl))
            for node in self.childNodes:
                node.writexml(writer,indent+addindent,addindent,newl)
            writer.write("%s</%s>%s" % (indent,self.tagName,newl))
        """
        doc = minidom.Document()
        script = doc.createElement("VehicleHunterTestScript")
        for case in self.testList:
            caseNode = case.makeXmlNode(doc)
            script.appendChild(caseNode)
        doc.appendChild(script)
        try:
            f = open(path, 'w')
            doc.writexml(f, encoding="utf-8", newl="\n", addindent="\t")
            f.close()
        finally:
            f.close()
            
    def load(self, path):
        doc = minidom.parse(path)
        script = doc.getElementsByTagName("VehicleHunterTestScript")[0]
        self.testList = []
        for caseNode in script.getElementsByTagName("testcase"):
            case = TestCase()
            case.parseXmlNode(caseNode)
            self.testList.append(case)

def saveTest(testList, path):
    tf = TestFlow()
    tf.setTestList(testList)
    tf.save(path)
    
def loadTest(path):
    tf = TestFlow()
    tf.load(path)
    return tf
    
if __name__ == '__main__':
    TestFlow().load("script/fff.test")
    